package system.base;

import java.sql.ResultSet;

import framework.core.*;
import system.SelectManager;
import system.data.*;

/*
 Author    :Wing
 CreateTime:2008-02-19 13:17:40
 */
public class ProductStorage extends ProductStorageBase {

    // ----system generated start----//
    public ProductStorage() {

    }
    
    
    
    public static String[] getProductCount(String warehouseCode,String productCode) {
        String sql = "  select  sum(PRODUCT_COUNT)  PRODUCT_COUNT from T_PRODUCT_STORAGE  where warehouse_code like '"+warehouseCode+"%' and PRODUCT_CODE ='"+productCode+"'";
        DBConnection con = null;
        try {
            con = DBConnection.open(false);
        } catch (Exception e1) {

            e1.printStackTrace();
        }
        DBPreparedStatement pst = null;
        ResultSet rs = null;
        String[] str = new String[1];
        
        try {
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()) {
                str[0] = rs.getString("PRODUCT_COUNT");
            
            }
            return str;
        } catch (Exception e) {
            e.printStackTrace();
            return str;
        } finally {
            try {
                if (pst != null)
                    pst.close();
            } catch (Exception e) {

                e.printStackTrace();
            }
            try {
                if (con != null)
                    con.close();
            } catch (Exception e) {

                e.printStackTrace();
            }
        }
    }
    
    
    
    public static String[] getMaxCount(String warehouseCode,String productCode) {
        String sql = " select MAX_COUNT from t_warehouse_max_storage_config where warehouse_code = '"+warehouseCode+"' and PRODUCT_CODE ='"+productCode+"'";
        DBConnection con = null;
        try {
            con = DBConnection.open(false);
        } catch (Exception e1) {

            e1.printStackTrace();
        }
        DBPreparedStatement pst = null;
        ResultSet rs = null;
        String[] str = new String[1];
        
        try {
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()) {
                str[0] = rs.getString("MAX_COUNT");
            
            }
            return str;
        } catch (Exception e) {
            e.printStackTrace();
            return str;
        } finally {
            try {
                if (pst != null)
                    pst.close();
            } catch (Exception e) {

                e.printStackTrace();
            }
            try {
                if (con != null)
                    con.close();
            } catch (Exception e) {

                e.printStackTrace();
            }
        }
    }
    

    public static String[] getSimpleProductStorage(String productStorageId) {
        String sql = "select product_price,provider_name from t_product_storage where id = '" + productStorageId + "'";
        DBConnection con = null;
        try {
            con = DBConnection.open(false);
        } catch (Exception e1) {

            e1.printStackTrace();
        }
        DBPreparedStatement pst = null;
        ResultSet rs = null;
        String[] str = new String[] { "", "" };
        
        try {
            pst = con.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()) {
                str[0] = rs.getString("product_price");
                str[1] = rs.getString("provider_name");
            }
            return str;
        } catch (Exception e) {
            e.printStackTrace();
            return str;
        } finally {
            try {
                if (pst != null)
                    pst.close();
            } catch (Exception e) {

                e.printStackTrace();
            }
            try {
                if (con != null)
                    con.close();
            } catch (Exception e) {

                e.printStackTrace();
            }
        }
    }

    private static void delete(DBConnection con, String id) throws Exception {
        Conditions conditions = new Conditions();

        conditions.add(ID, id);

        delete(con, TABLE_NAME, conditions);
    }

    public static ProductStorage fetch() {
        return new ProductStorage();
    }

    public static ProductStorage copy(ProductStorage srcObj) {
        ProductStorage obj = fetch();
        obj.copyBase(srcObj);
        return obj;
    }

    public static ProductStorage fetch(DBConnection con, String id) throws Exception {
        Conditions conditions = new Conditions();

        conditions.add(ID, id);

        return fetch(con, conditions);
    }

    public static ProductStorage fetch(DBConnection con, Conditions conditions) throws Exception {
        DBResult result = null;
        try {
            result = DBEngine.doSelect(con, "select * from " + TABLE_NAME, conditions);
            java.sql.ResultSet rs = result.getRs();
            ProductStorage obj = null;
            if (rs.next()) {
                obj = fetch(rs);
            }
            return obj;
        } finally {
            if (result != null)
                result.clear();
        }
    }

    public static ProductStorage fetch(java.sql.ResultSet rs) throws Exception {
        ProductStorage obj = new ProductStorage();
        obj.initObject(rs);
        return obj;
    }

    protected void getUnique(DBConnection con) throws Exception {
        setId(DBEngine.getId(con));
    }

    // ----system generated end----//

    // ----add by Wing start----//
    public static boolean IsCheckedOfWarehouseCode(String loginWarehouseCode, String warehouseCode) throws Exception {
        boolean checked = false;
        if (loginWarehouseCode != null) {
            if (loginWarehouseCode.equals(warehouseCode))
                return true;
        }
        return checked;
    }

    public static int getDullStorageTimes(String lastUpdateTime) {
        int dullTimes = 0;
        DateUtil dateUtil1 = new DateUtil();
        dateUtil1.advanceDays(-60 + 1);
        String date1 = dateUtil1.getFullString();
        DateUtil dateUtil2 = new DateUtil();
        dateUtil2.advanceDays(-90 + 1);
        String date2 = dateUtil2.getFullString();
        DateUtil dateUtil3 = new DateUtil();
        dateUtil3.advanceDays(-180 + 1);
        String date3 = dateUtil3.getFullString();
        if (lastUpdateTime.compareTo(date3) <= 0) {
            dullTimes = 180;
        } else if (lastUpdateTime.compareTo(date2) <= 0) {
            dullTimes = 90;
        } else if (lastUpdateTime.compareTo(date1) <= 0) {
            dullTimes = 60;
        }
        return dullTimes;
    }

    // ----add by Wing end----//

    // add by Jackey start --//
    public String viewWarehouseName(DBConnection con) throws Exception {
        String mReturn = "";
        Warehouse w = Warehouse.fetchByCode(con, getWarehouseCode());
        if (w != null) {
            mReturn = w.getName();
        }
        return mReturn;
    }

    public String viewProductCount(DBConnection con) throws Exception {
        return StringUtil.showCount(String.valueOf(getProductCount()));
    }

    // add by Jackey end

    public static double getOrderCount(DBConnection con, String warehouseCode, String concentricityCode) throws Exception {
        DBResult result = null;
        double num = 0;
        try {
            String sql = "Select sum(GS_ORDER_COUNT) total from GS_ORDER_DETAIL sub " +
                         "left outer join GS_ORDER head on sub.GS_ORDER_TEMPID = head.GS_ORDER_TEMPID " +
                         "where " +
                         "head.GS_WORKCORP_ID = '" + warehouseCode + "' " +
                         " and upper(sub.GS_CONCENTRICITY_ID) = '" + concentricityCode.toUpperCase() + "'" +
                         " and sub.GS_ORDER_DETAIL_STATUS in ('6','3','7','9','10','12')";
            
            result = DBEngine.doSelect(con, sql);
            java.sql.ResultSet rs = result.getRs();
            
            if (rs.next()) {
                num = rs.getDouble("total");
            }
            return num;
        } finally {
            if (result != null)
                result.clear();
        }
    }

}
